#!/usr/bin/python
# coding=utf-8

import urllib2
import urllib
import base64
import sys
import json

def up(status):
    theurl = ' http://api.zuosa.com/statuses/update.json'
    req = urllib2.Request(theurl)
    values = {'status' : status}
    data = urllib.urlencode(values)
    username = ' '
    password = ' '
    base64string = base64.encodestring(
                '%s:%s' % (username, password))[:-1]
    authheader =  "Basic %s" % base64string
    req.add_header("Authorization", authheader)
    handle = urllib2.urlopen(req, data)
    thepage = handle.read()

arg = sys.argv[1]
if arg == 'up':
    status = ' '.join(sys.argv[2:])
    up(status)
elif arg == 'dn':
    name = sys.argv[2]
    theurl = 'http://api.zuosa.com/statuses/user_timeline.json?id=%s'
    status = urllib.urlopen(theurl % name).read()
    status = json.loads(status)
    moban = u'%s:%s'
    for i in status:
        print moban % (i['created_at'], i['text'])
elif arg == 'dj':
    theurl = 'http://api.zuosa.com/statuses/public_timeline.json'
    status = urllib.urlopen(theurl).read()
    status = json.loads(status)
    moban = u'%s:%s'
    for i in status:
        print moban % (i['created_at'], i['text'])
else:
    status = ' '.join(sys.argv[1:])
    up(status)
